package file;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import file.iobase.FileRead;
import svn.SVNUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 史祥连
 * @Date 2023/8/25 16:43
 * @Version 1.0
 */
public enum AutoCommit {

    kb_231001("C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\231001.txt"),
    kb_231002("C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\231002.txt"),
    kb_231003("C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\231003.txt"),
    kb_test("C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\test.txt");
    private String sourceFileListPath;
    private static String MoBanKu = "C:\\Datas\\E9\\2310模板\\模板库";
    private static String YuanShiKu = "C:\\Datas\\E9\\2310模板\\2310原始模板库";

    enum DbType {
        DM,
        GS,
        JC,
        Mysql,
        Oracle,
        PG,
        SQLServer,
        ST;

    }
    private  String bakFile = "\\\\192.168.7.108\\ECOLOGY_customer\\S\\S深圳市卡司通展览股份\\历史文件\\ecology";
    private  DbType dbType = DbType.SQLServer;
    private  String proPath = "C:\\Datas\\E9\\客户分支\\S深圳市卡司通展览股份";

    private  String svnPro = "https://192.168.0.71/svn/S深圳市卡司通展览股份/";



    AutoCommit(String sourceFileListPath) {
        this.sourceFileListPath = sourceFileListPath;
    }


    public static void main(String[] args) throws Exception {
        commit();
    }

    private static void commit() throws Exception {
        String bakFile = "\\\\192.168.7.108\\ECOLOGY_customer\\Z\\Z中国兵器工业集团207研究所\\历史文件\\ecology";
        DbType dbType = DbType.Oracle;
        String svnPro = "https://192.168.0.71/svn/Z中国兵器工业集团207研究所/";


        autoCommit(svnPro,kb_231001,bakFile,dbType,"2310已知问题处理");
    }

    private static void autoCommit(String svnPro, AutoCommit kb, String bakFile, DbType dbType, String commitMsg) throws Exception {
        //checkout代码到本地
        String proPath = SVNUtil.checkout(svnPro);

        //将代码移过来
        copyFilesAndRenameSql(kb
                , bakFile
                ,proPath
                /*,DbType.SQLServer
                ,true*/
        );

        //比较不一致的文件，并且删除该文件
        boolean ifEquals = isEquals(proPath,true);
        if (!ifEquals) {
            //return;
        }


        //将要更新的类 add 到svn库
        SVNUtil.add(proPath);

        SVNUtil.commit(proPath,commitMsg+"-初始化");
        copyFilesAndRenameSql(kb
                , bakFile
                ,proPath
                ,dbType
                ,true
        );
        SVNUtil.commit(proPath,commitMsg+"-提交");
    }

    private static boolean isEquals(String proPath,boolean isDelete) throws Exception {
        String[] split = proPath.split("\\\\");
        String kehuChineseName = split[split.length - 1].substring(1);

        List<String> strings;
        String coverPath = "C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\" + kehuChineseName + ".txt";
        strings = FileRead.readFile(coverPath);
        boolean ifEquals = true;
        for (String string : strings) {
             ifEquals = FileComparator.areFilesEqual(YuanShiKu + string, proPath + "\\" + string);
            if (!ifEquals) {
                if (isDelete) {
                    File file = new File(proPath + "\\" + string);
                    file.delete();
                }
                System.out.println("与模板库不一致文件：：： " + string);
            }
        }
        return ifEquals;
    }

    private static void copyFilesAndRenameSql(AutoCommit kb, String sourceTitle, String targetTitle) throws Exception {
        copyFilesAndRenameSql(kb, sourceTitle, targetTitle, null,false);

    }

    private static void copyFilesAndRenameSql(AutoCommit kb, String sourceTitle, String targetTitle, DbType dbType, boolean needCover) throws Exception {

        String[] split = targetTitle.split("\\\\");
        String kehuChineseName = split[split.length - 1].substring(1);

        List<String> strings = FileRead.readFile(kb.sourceFileListPath);
        String coverPath = "C:\\Users\\LENOVO\\AppData\\Roaming\\JetBrains\\IntelliJIdea2022.3\\scratches\\dangan\\" + kehuChineseName + ".txt";
        if (needCover) {
            sourceTitle = MoBanKu;
            strings = FileRead.readFile(coverPath);
        } else {
            strings = FileRead.readFile(kb.sourceFileListPath);
        }
        List<String> existsFile = new ArrayList<>();
        List<String> neddFile = new ArrayList<>();
        for (String string : strings) {
            File fileTest = new File(targetTitle + string);
            if (!needCover) {
                if (fileTest.exists()) {
                    existsFile.add("++++++++++++++++++++++++++++文件已存在：\t" + targetTitle + string);
                } else {
                    FileCopy.copyFile(sourceTitle + string, targetTitle + string, needCover);
                    neddFile.add(string);
                }
            } else {
                FileCopy.copyFile(sourceTitle + string, targetTitle + string, needCover);
            }
        }
        if (!needCover) {
            writeNeedFileList(coverPath, neddFile);
        }
        for (String s : existsFile) {
            System.out.println(s);
        }
        // 多语言脚本
        if (AutoCommit.kb_231001 == kb && dbType != null) {
            copySql(targetTitle, dbType);

        }
    }

    private static void writeNeedFileList(String coverPath, List<String> neddFile) throws IOException {
        File file = new File(coverPath);
        if (file.exists()) {
            file.delete();
        }
        FileWriter fileWriter = new FileWriter(coverPath);
        for (String nedd : neddFile) {
            fileWriter.write(nedd+"\n");
            fileWriter.flush();
        }
    }

    private static void copySql(String targetTitle, DbType dbType) {
        String db = dbType.toString();
        File file = new File(targetTitle + "\\sqlupgrade\\");
        if (!file.exists()) {
            file.mkdirs();
        }
        FileUtil.copy("C:\\Datas\\E9\\业务id重置\\sqlupgrade\\" + db, targetTitle + "\\sqlupgrade\\", false);
        File sqlfile = new File(targetTitle + "\\sqlupgrade\\" + db + "\\sql202311160201.sql");
        String pinyin = getPinyin(targetTitle);
        FileUtil.rename(sqlfile, "sql202311160201" + pinyin + ".sql", true);
    }

    private static String getPinyin(String targetTitle) {
        String[] split = targetTitle.split("\\\\");
        String kehuChineseName = split[split.length - 1].substring(1);
        String pinyin = PinyinUtil.getFirstLetter(kehuChineseName, "");
        pinyin = pinyin.toUpperCase();
        return pinyin;
    }
}
